package com.keekus.common.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.MessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.ui.context.Theme;
import org.springframework.ui.context.ThemeSource;
import org.springframework.ui.context.support.SimpleTheme;

public class MyThemeSource implements ThemeSource {

	private String basenamePrefix = "com.keekus.common.themes.";

	private final Map<String, Theme> themeCache = new HashMap();
	public static final String DEFAUT_STYLE = "style1";
	private String defaultStyle = "style1";

	public String getDefaultStyle() {
		return this.defaultStyle;
	}

	public void setDefaultStyle(String defaultStyle) {
		this.defaultStyle = defaultStyle;
	}

	public Theme getTheme(String themeName) {
		if (themeName == null) {
			return null;
		}
		synchronized (this.themeCache) {
			Theme theme = (Theme) this.themeCache.get(themeName);
			if (theme == null) {
				String basename = this.basenamePrefix + themeName;
				MessageSource messageSource = createMessageSource(basename);
				theme = new SimpleTheme(themeName, messageSource);

				this.themeCache.put(themeName, theme);
			}
			return theme;
		}
	}

	protected MessageSource createMessageSource(String basename) {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasename(basename);
		return messageSource;
	}
}